package com.ibox.core.main;

import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ibox.BaseApplication;
import com.ibox.base.BaseActivity;
import com.ibox.common.OnRvItemClickListener;
import com.ibox.utils.SnackBarUtils;
import com.ibox.widget.SupportDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    Toolbar mTbMain;
    RecyclerView mRvMain;
    // 退出时间
    private long currentBackPressedTime = 0;
    // 退出间隔
    private static final int BACK_PRESSED_INTERVAL = 2000;
    private List<MainBean> mMainBeanList;
    private MainAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mTbMain = findView(R.id.tbMain);
        mRvMain = findView(R.id.rvMain);
    }

    @Override
    public void initData() {

        setSupportActionBar(mTbMain);

        mMainBeanList = new ArrayList<>();
        mMainBeanList.add(new MainBean("小说阅读器", R.drawable.home_book_reader));
        mMainBeanList.add(new MainBean("代码阅读器", R.drawable.home_code_reader));
        mMainBeanList.add(new MainBean("音乐", R.drawable.home_music));
        mMainBeanList.add(new MainBean("福利", R.drawable.home_welfare));
        mMainBeanList.add(new MainBean("搞笑段子", R.drawable.home_funny));
        mAdapter = new MainAdapter(this, mMainBeanList);

        mRvMain.setHasFixedSize(true);
        mRvMain.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        mRvMain.addItemDecoration(new SupportDividerItemDecoration(this, LinearLayoutCompat.VERTICAL));
        mRvMain.addItemDecoration(new SupportDividerItemDecoration(this, LinearLayoutManager.VERTICAL, true));

        mRvMain.setAdapter(mAdapter);
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - currentBackPressedTime > BACK_PRESSED_INTERVAL) {
                SnackBarUtils.showSnackbar(mRvMain, "再按一次退出应用");
                return true;
            } else {
                BaseApplication.getInstance().exitApp(); // 退出
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void configViews() {
        mAdapter.setItemClickListener(new OnRvItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                switch (position) {
                    case 0:
                        ARouter.getInstance().build("/main/book").navigation();
                        break;
                }
            }
        });

    }
}
